package com.springboot.cloud.demos.web.controller;

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.demos.entity.form.AppVersionForm;
import com.springboot.cloud.demos.entity.po.AppVersion;
import com.springboot.cloud.demos.web.service.AppVersionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
public class AppVersionController {
    @Autowired
    private AppVersionService appVersionService;


    @GetMapping("/appVersion/list")
    public String appVersion(Model model) {

        Result result = appVersionService.list();
        if (result.isSuccess()) {
            model.addAttribute("appVersions", result.getData());
        }
        return "appVersion/list";
    }

    @GetMapping("/appVersion/input")
    public String input(String id, Model model) {
        if (StringUtils.isNotEmpty(id)) {
            Result result  = appVersionService.queryById(id);
            model.addAttribute("appVersion", result.getData());
            model.addAttribute("isEdit", "edit");
        } else {
            model.addAttribute("appVersion", new AppVersion());
            model.addAttribute("isEdit", "add");
        }
        return "appVersion/input";
    }

    @RequestMapping(value = "/appVersion/save", method = RequestMethod.POST)
    @ResponseBody
    public Result save(AppVersionForm appVersionForm) {
        if (StringUtils.isNotEmpty(appVersionForm.getId())) {
            return appVersionService.update(appVersionForm);
        }
        return appVersionService.add(appVersionForm);
    }

    @RequestMapping(value = "/appVersion/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@PathVariable("id")String appVersionId) {
        return appVersionService.delete(appVersionId);
    }


}
